#include <iostream>
#include <math.h>
using namespace std;
class security
{
	public:
		long getpublickey(long a,long b,long c);
		long fn(long M,long ed,long N);
		long fn(const char *in_buff,long *out_buff,long ed,long n);
		long fn(const long *in_buff,char *out_buff,long ed,long n);
		long char_to_hex(const long *in_buff,char *out_buff);
		
};
long security::getpublickey(long p,long q,long e)
{
	long n,t;
	long d;
	n=p*q;
	t=(p-1)*(q-1);
	for(d=1;d<9999;d++)
	{
		if((((e*d)%t==1))&&(d!=e))
		{
			return d;
			break;
		}
	}
}

long security::fn(long Msg,long key,long N)
{
	long r=1;
	key+=1;
	while(key!=1)
	{
		r=r*Msg;
		r=r%N;
		key--;
	}
	return r;

}
long security::fn(const long* in_buff,char *out_buff,long ed,long n)
{

	char * temp;
	temp=new char[1024];
	while(*in_buff!='\0')
	{
		*out_buff++=fn(*in_buff,ed,n);
		in_buff++;
	}
	*out_buff='\0';
}

long security::fn(const char *in_buff,long *out_buff,long ed,long n)
{

	cout<<"in_buff"<<in_buff<<endl;
	char * temp;
	temp=new char[1024];
	while(*in_buff!='\0')
	{
		
		*out_buff=fn(*in_buff,ed,n);
		sprintf(temp,"%s%04x",temp,*out_buff);
		out_buff++;
		in_buff++;
	}
	cout<<"message encode:"<<temp<<endl;

}

//long security::char_to_hex(const long* in_buff,char * out_buff){
//	char * a=out_buff;
//	while(*in_buff++!='\0'){
//		sprintf(out_buff,"%s%02x",out_buff,in_buff);	
//	}
//}
int main()
{
	long p,q,e,c,msg;
	char m[1024];
	long outbuff[1024];
	char sm[1024];
	char jm[1024];

	security s;
	p=47;
	q=53;
	e=61;
	long n;
	n=p*q;
	long d;
	d=s.getpublickey(p,q,e);

	
	cout<<"d is "<<d<<endl;
	
	cout<<"print m is ";
	
	cin>>m;
	
//	c=s.fn(m,d,n);
	s.fn(m,outbuff,d,n);
	//cout<<*outbuff<<endl;
//	s.char_to_hex(outbuff,sm);
//	cout<<"get c is "<<sm<<endl;
	
	s.fn(outbuff,jm,e,n);

//	msg=fn(c,e,n);
//	s.char_to_hex(jm,sm);
	cout<<"get M is "<<jm<<endl;

	return 0;
}


